package com.declum.archon.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.apache.mina.common.ByteBuffer;

public class PingRequest extends BaseRequest {
	String	PING_REQUEST	= "TYPE_XPING";

	public String sendPingRequest() throws IOException {
		DataOutputStream out = new DataOutputStream(this.socket
				.getOutputStream());
		out.write(this.PING_REQUEST.getBytes());
		DataInputStream in = new DataInputStream(this.socket.getInputStream());

		ByteBuffer buffer = ByteBuffer.allocate(1024);
		byte[] data = new byte[1024];
		int totalCount = 0;
		while (true) {
			int read = in.read(data);
			if (read == -1) {
				break;
			}
			buffer.put(data);
			totalCount += read;
		}

		buffer.flip();
		buffer.get(data, 0, totalCount);
		return (new String(data, 0, totalCount));
	}

	public static void main(String[] args) throws IOException {
		PingRequest request = (PingRequest) RequestFactory.getInstance()
				.getRequestInstance(RequestType.PING);
		request.connect("localhost", 12123);
		System.out.println(request.sendPingRequest());
	}
}
